<?php
class access_Core
{

	public static function require_post()
	{
		request::method() == 'post' OR die('post only');
	}

	public static function require_login()
	{
		user::active()->login OR error_403();
	}

	public function forbidden()
	{
		if(!user::active()->login)
		{
			ajax::response('error',Kohana::lang('error.access_deny'));
		}
		else
		{
			throw new Kohana_Exception('error.access_deny');
		}
	}

	static function verify_csrf() 
	{
		$input = Input::instance();
		if ($input->post("csrf", $input->get("csrf", null)) !== Session::instance()->get("csrf")) 
		{
			return false;
		}
		return true;
	}

	static function csrf_token() 
	{
		$session = Session::instance();
		$csrf = $session->get("csrf");
		if (empty($csrf)) {
			$csrf = md5(rand());
			$session->set("csrf", $csrf);
		}
		return $csrf;
	}

	static function csrf_form_field() 
	{
		return "<input type=\"hidden\" name=\"csrf\" value=\"" . self::csrf_token() . "\"/>";
	}
}
